//
//  HHConversationCell.m
//  HHChatKit
//
//  Created by Henry on 2021/5/10.
//

#import "HHConversationCell.h"
#import <HHTool/HHTool.h>
#import "HHConversationModel.h"
#import "NSDate+HHChat.h"
#import "UIImageView+HHChat.h"
#import "HHBadge.h"

@interface HHConversationCell ()

@property (nonatomic, strong) HHBadge *badge;

@end

@implementation HHConversationCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.separatorInset = UIEdgeInsetsMake(0, 12, 0, 0);
        [self p_initViews];
    }
    return self;
}

- (void)p_initViews {
    _avatarView = [[UIImageView alloc] init];
    _nameLabel = [[UILabel alloc] init];
    _msgLabel = [[UILabel alloc] init];
    _timeLabel = [[UILabel alloc] init];
    _badge = [[HHBadge alloc] init];

    [self.contentView addSubview:_avatarView];
    [self.contentView addSubview:_nameLabel];
    [self.contentView addSubview:_msgLabel];
    [self.contentView addSubview:_timeLabel];
    [self.contentView addSubview:_badge];

}

- (void)layoutSubviews {
    [super layoutSubviews];
        
    HHConversationCellConfig *config = self.model.config;
    
    CGFloat imgHeight = config.nameFont.lineHeight + config.msgFont.lineHeight + config.margin;

    self.avatarView.mm_width(imgHeight).mm_height(imgHeight).mm_left(config.margin).mm_top(config.margin);

    CGFloat textMargin = config.margin / 4;
    self.nameLabel.mm_top(self.avatarView.mm_y + textMargin).mm_left(self.avatarView.mm_maxX + config.margin).mm_height(config.nameFont.lineHeight);
    self.timeLabel.mm_sizeToFit().mm_height(config.timeFont.lineHeight).mm__centerY(self.nameLabel.mm_centerY).mm_right(config.margin);
    self.nameLabel.mm_flexToRight(self.timeLabel.mm_w+self.timeLabel.mm_r + 10);
    
    self.msgLabel.mm_left(self.nameLabel.mm_x).mm_height(config.msgFont.lineHeight).mm_top(self.nameLabel.mm_maxY + config.margin / 2);
    self.badge.mm__centerY(self.msgLabel.mm_centerY).mm_right(config.margin);
    self.msgLabel.mm_flexToRight(self.contentView.mm_w-self.badge.mm_x + 10);
}

- (void)setModel:(HHConversationModel *)model {
    _model = model;
    [_avatarView hh_cornerRadius:model.config.avatarCorner];
    _avatarView.image = [UIImage hh_imageWithColor:model.config.avatarColor];

    _nameLabel.text = [model getConversationTitle];
    _msgLabel.text = model.msg ?: @"";
    
    _timeLabel.text = [NSDate shortTimeMessageString:model.time week:model.config.showWeekday];
    
    _badge.badgeValue = model.badgeValue;
    
    [_avatarView hh_setConvImage:model];
    
    if (model.top) {
        self.backgroundColor = model.config.cellTopColor;
    } else {
        self.backgroundColor = model.config.cellColor;
    }
    
    self.separatorInset = model.config.separatorInset;
    
    _nameLabel.textColor = model.config.nameColor;
    _msgLabel.textColor = model.config.msgColor;
    _timeLabel.textColor = model.config.timeColor;
    
    _badge.titleColor = model.config.badgeTextColor;
    _badge.backgroundColor = model.config.badgeColor;
    
    [self setupFont];
}

- (void)setupFont {
    _nameLabel.font = self.model.config.nameFont;
    _msgLabel.font = self.model.config.msgFont;
    _timeLabel.font = self.model.config.timeFont;
    _badge.titleFont = self.model.config.badgeFont;
}

+ (NSString *)identifier {
    return NSStringFromClass([HHConversationCell class]);
}

+ (void)registerCellToTableView:(UITableView *)tableView {
    [tableView registerClass:[HHConversationCell class] forCellReuseIdentifier:[HHConversationCell identifier]];
}

+ (instancetype)dequeueOrCreateCellByTableView :(UITableView *)tableView {
    HHConversationCell *cell = [tableView dequeueReusableCellWithIdentifier:[HHConversationCell identifier]];
    if (cell == nil) {
        cell = [[HHConversationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[[self class] identifier]];
    }
    return cell;
}

+ (CGFloat)viewHeight:(HHConversationModel *)model {
    CGFloat imgHeight = model.config.nameFont.lineHeight + model.config.msgFont.lineHeight + model.config.margin;
    return imgHeight + model.config.margin + model.config.margin;
}

@end
